/*******************************************************************************
* @Filename      : bsp_sys.c

* @Description   : bsp_sys.c

* @Version       : 1.0.0

* @Date          : 2023.09.25
*******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "bsp_sys.h"

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/
#define SYS_MS_TIM_IRQn      TIM14_IRQn
#define SYS_MS_TIM_PERIOD    (1000 - 1)
#define SYS_MS_TIM_PRESCALER (RCC_GetPCLK2Freq() / 1000000 - 1)

/* Global variables ----------------------------------------------------------*/
__IO uint32_t BSP_SYS_Tick = 0;
__IO uint32_t BSP_SYS_1MS_Flag = 0;
__IO uint32_t BSP_SYS_2MS_Tick = 0;
__IO uint32_t BSP_SYS_2MS_Flag = 0;
__IO uint32_t BSP_SYS_40MS_Tick = 0;
__IO uint32_t BSP_SYS_40MS_Flag = 0;


__IO uint32_t  BSP_SYS_US_Tick = 0;
/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/
static void SYS_CLK_Init(void);
static void SYS_MS_TIM_Init(void);
//static void SYS_UC_PIN_Init(void);

/* Exported functions --------------------------------------------------------*/
/*******************************************************************************
* @Name      : BSP_SYS_Init
* @Purpose   : BSP_SYS_Init
* @Param     : None
* @Retval    : None
*******************************************************************************/
void BSP_SYS_Init(void)
{
    SYS_CLK_Init();
//    SYS_UC_PIN_Init();
    SYS_MS_TIM_Init();
//    DelayMs(500);
}

/*******************************************************************************
* @Name      : BSP_SYS_MS_Delay
* @Purpose   : BSP_SYS_MS_Delay
* @Param     : None
* @Retval    : None
*******************************************************************************/
void BSP_SYS_MS_Delay(uint32_t ms)
{
    uint32_t TickNow;
    TickNow = GetMsTick();
    while((GetMsTick() - TickNow) < ms)
    {
//        BSP_WDG_Reload();
    }
}

void BSP_SYS_US_Delay(uint32_t us)
{
    uint32_t TickNow;
    TickNow = GetUsTick();
    while((GetUsTick() - TickNow) < us)
    {
//        BSP_WDG_Reload();
    }
}
/* Overrride functions -------------------------------------------------------*/

/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* @Name      : SYS_CLK_Init
* @Purpose   : SYS_CLK_Init
* @Param     : None
* @Retval    : None
*******************************************************************************/
static void SYS_CLK_Init(void)
{
    RCC_APB2PeriphClockCmd(RCC_APB2ENR_SYSCFG, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1ENR_PWR, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOA, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOB, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOC, ENABLE);
    RCC_AHBPeriphClockCmd(RCC_AHBENR_GPIOD, ENABLE);
}

/*******************************************************************************
* @Name      : SYS_UC_PIN_Init
* @Purpose   : SYS_UC_PIN_Init
* @Param     : None
* @Retval    : None
*******************************************************************************/
//static void SYS_UC_PIN_Init(void)
//{
//    GPIO_InitTypeDef  GPIO_InitStruct;

//    GPIO_StructInit(&GPIO_InitStruct);
//    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

//    GPIO_InitStruct.GPIO_Pin  = UC_GPIOA_PIN;
//    GPIO_InitStruct.GPIO_Mode = UC_GPIOA_MODE;
//    GPIO_Init(UC_GPIOA_PORT, &GPIO_InitStruct);

//    GPIO_InitStruct.GPIO_Pin  = UC_GPIOB_PIN;
//    GPIO_InitStruct.GPIO_Mode = UC_GPIOB_MODE;
//    GPIO_Init(UC_GPIOB_PORT, &GPIO_InitStruct);
//}

/*******************************************************************************
* @Name      : SYS_MS_TIM_Init
* @Purpose   : SYS_MS_TIM_Init
* @Param     : None
* @Retval    : None
*******************************************************************************/
static void SYS_MS_TIM_Init(void)
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
    NVIC_InitTypeDef        NVIC_InitStruct;

    RCC_APB2PeriphClockCmd(RCC_APB2ENR_TIM14, ENABLE);

    TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
    TIM_TimeBaseInitStruct.TIM_Prescaler         = SYS_MS_TIM_PRESCALER;
    TIM_TimeBaseInitStruct.TIM_CounterMode       = TIM_CounterMode_Up;
    TIM_TimeBaseInitStruct.TIM_Period            = SYS_MS_TIM_PERIOD;
    TIM_TimeBaseInitStruct.TIM_ClockDivision     = TIM_CKD_DIV1;
    TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(SYS_MS_TIM, &TIM_TimeBaseInitStruct);

    TIM_ClearFlag(SYS_MS_TIM, TIM_FLAG_Update);
    TIM_ITConfig(SYS_MS_TIM, TIM_IT_Update, ENABLE);

    NVIC_InitStruct.NVIC_IRQChannel         = SYS_MS_TIM_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPriority = 0x01;
    NVIC_InitStruct.NVIC_IRQChannelCmd      = ENABLE;
    NVIC_Init(&NVIC_InitStruct);

    TIM_Cmd(SYS_MS_TIM, ENABLE);
}


///////////////////////////////////////////////////////

void TIM3_Configure(void)
{
    NVIC_InitTypeDef        NVIC_InitStruct;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;

    RCC_ClocksTypeDef RCC_Clocks;

    RCC_GetClocksFreq(&RCC_Clocks);

    RCC_APB1PeriphClockCmd(RCC_APB1ENR_TIM3, ENABLE);

    TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
    TIM_TimeBaseInitStruct.TIM_Prescaler         = (RCC_GetPCLK2Freq() / 1000 - 1);//(RCC_Clocks.PCLK1_Frequency / 10000 - 1);
    TIM_TimeBaseInitStruct.TIM_CounterMode       = TIM_CounterMode_Up;
    TIM_TimeBaseInitStruct.TIM_Period            = SYS_MS_TIM_PERIOD;//(1000 - 1);
    TIM_TimeBaseInitStruct.TIM_ClockDivision     = TIM_CKD_DIV1;
    TIM_TimeBaseInitStruct.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);

    TIM_ClearFlag(TIM3, TIM_FLAG_Update);
    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);

    NVIC_InitStruct.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStruct.NVIC_IRQChannelPriority = 0x01;
    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStruct);

    TIM_Cmd(TIM3, ENABLE);
}

/* End of File ---------------------------------------------------------------*/
